<?php

namespace App\Http\Controllers\Api\v2;

use App\Models\User;;
use App\Http\Controllers\Controller;
use App\Models\UserGroup;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    //
    public function index()
    {
        $username = $this->request->input('username');
        $mobile   = $this->request->input('mobile');
        $password = $this->request->input('password');

        if (User::where('username', $username)->exists())
        {
            return ajaxError(1, trans('user.username be occupied'));
        }

        if (User::where('mobile', $mobile)->exists()){
            return ajaxError(2, trans('user.mobile be occupied'));
        }

        $user = User::create([
            'username' => $username,
            'mobile' => $mobile,
            'password' => Hash::make($password),
            'created_at' => time(),
            'gid'=>UserGroup::userGroup()->first()->gid
        ]);

        $user->info()->create();
        $user->stat()->create();
        //写入日志
        $user->logs()->create([
            'ip'=>$this->request->getClientIp(),
            'operate'=>'register',
            'created_at'=>time(),
            'src'=>'app'
        ]);

        return ajaxReturn(['user'=>$user]);
    }
}
